//
//  IMLoginModels.m
//  DualMediationSDK
//
//  Created by YeJary on 2025/6/23.
//

#import "IMLoginModels.h"

@implementation IMLoginRequest

+ (instancetype)requestWithAppId:(NSString *)appId 
                        iosUuid:(NSString *)iosUuid 
                    countryCode:(NSString *)countryCode {
    IMLoginRequest *request = [[IMLoginRequest alloc] init];
    request.app_id = appId;
    request.ios_uuid = iosUuid;
    request.country_code = countryCode;
    return request;
}

- (NSDictionary *)toDictionary {
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    if (self.app_id) {
        params[@"app_id"] = self.app_id;
    }
    
    if (self.ios_uuid) {
        params[@"ios_uuid"] = self.ios_uuid;
    }
    
    if (self.country_code) {
        params[@"country_code"] = self.country_code;
    }
    
    return [params copy];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: %p> app_id:%@ ios_uuid:%@ country_code:%@",
            NSStringFromClass([self class]), self, self.app_id, self.ios_uuid, self.country_code];
}

@end

@implementation IMLoginResponseData

+ (instancetype)dataFromDictionary:(NSDictionary *)dictionary {
    if (!dictionary || ![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    IMLoginResponseData *data = [[IMLoginResponseData alloc] init];
    data.uuid = dictionary[@"uuid"];
    data.reg_ts = dictionary[@"reg_ts"];
    
    return data;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: %p> uuid:%@ reg_ts:%@",
            NSStringFromClass([self class]), self, self.uuid, self.reg_ts];
}

@end

@implementation IMLoginResponse

+ (instancetype)responseFromDictionary:(NSDictionary *)dictionary {
    if (!dictionary || ![dictionary isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    IMLoginResponse *response = [[IMLoginResponse alloc] init];
    response.code = [dictionary[@"code"] integerValue];
    response.message = dictionary[@"message"] ?: @"";
    
    NSDictionary *dataDict = dictionary[@"data"];
    if (dataDict && [dataDict isKindOfClass:[NSDictionary class]]) {
        response.data = [IMLoginResponseData dataFromDictionary:dataDict];
    }
    
    return response;
}

- (BOOL)isSuccess {
    return self.code == 200;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"<%@: %p> code:%ld message:%@ data:%@",
            NSStringFromClass([self class]), self, (long)self.code, self.message, self.data];
}

@end